package edu.ccut.saturn.studio.logic.model.xml;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

/**
 * 
 * 
 * @author LeslieGu
 *
 */
public abstract class FileParseUtils {

	private static final String FILE_PARSE_UTILS_TWO_DOT = ":";

	/**
	 * 解析文件，根据key解析文件
	 * (bundlesybolicname)
	 * @param fullFilePath
	 * @param keyInLine
	 * @return
	 */
	
	public static String getParseStr(String fullFilePath, String keyInLine){
		BufferedReader bufferedReader = null;
		String line = null;
		try {
			bufferedReader = new BufferedReader(new FileReader(new File(fullFilePath)));
			
			while ((line = bufferedReader.readLine()) != null) {
				String parseStr = parseLineStr(line, keyInLine);
				if (parseStr != null) {
					return parseStr.trim();
				}
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (bufferedReader != null) {
				try {
					bufferedReader.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		
		return null;
	}
	
	private static String parseLineStr(String line, String key) {
		int index = line.indexOf(FILE_PARSE_UTILS_TWO_DOT);
		
		String lineKey = null;
		if (index != -1) {
			lineKey =  line.substring(0, index).trim();
			
			if (lineKey != null && lineKey.equalsIgnoreCase(key)) {
				return line.substring(index + 1).trim();
			}
		} 

		return null;
	}
	
	/*public static void main(String[] args) {
		String path = "d:\\MANIFEST.MF";
		String key = getParseStr(path, "Bundle-SymbolicName");
		System.out.println(key);
	}*/
}
